#include "Config.h"
#include "MsgHandler.h"
#include "MsgPool.h"

void MsgHandler::DeregisterAllMsg()
{
	auto itr = _handlerList.begin();
	for( ; itr != _handlerList.end(); ++ itr )
	{
		SAFE_DELETE( itr->second );
	}
	_handlerList.clear();
}

bool MsgHandler::ProcessMsg()
{
	Handler* handler = nullptr;
	MsgQueue tmp;
	MsgQueue msgQueue;
	if(!MSGPOOL().getMsgQueue( _worker, msgQueue ))
		return false;
	MsgHdr* hdr = nullptr;
	do
	{
		hdr = msgQueue.Pop();
		auto found = _handlerList.find( hdr->_cmd );
		if ( found != _handlerList.end() )
		{
			handler = found->second;
			handler->_wrapper( handler->_func, hdr );
		}
		else
		{
			//ERROR
		}
		delete[] (char *)hdr;
	}
	while (!msgQueue.Empty());
	return true;
}
